﻿<?php
class Model_Orderdetail extends Zend_Db_Table_Abstract
{
	protected $_name = 'order_detail';
	protected $_primary = 'od_id';
	
	public function createOrderdetail ($p_id,$o_id,$pod_soluong,$op_gia)
	{
		$rowOderDetail= $this->createRow();
		if ($rowOderDetail) {
			// update the row values
			$rowOderDetail->p_id = $p_id;
			$rowOderDetail->o_id = $o_id;
			$rowOderDetail->pod_soluong = $pod_soluong;
			$rowOderDetail->op_gia = $op_gia;
			$rowOderDetail->save();			
			//return the new Order			
			return $rowOderDetail; 
		} else {
			throw new Zend_Exception("Không tạo được đơn hàng !");
		}
	}  
	
	public static function getOrderDetailById($od_id)
	{
		$orderDetailModel = new self();
		$select = $orderDetailModel->select();
		$select->order(array('od_id'))
			   ->where('od_id =?',$od_id);	
		return $orderDetailModel->fetchAll($select);
	}
	public static function getOrderDetailByOrderId($o_id)
	{
		$orderDetailModel = new self();
		$select = $orderDetailModel->select();
		$select->order(array('od_id'))
			   ->where('o_id =?',$o_id);	
		return $orderDetailModel->fetchAll($select);
	}
	
	public static function getOrderDetailAll()
	{
		$orderDetailModel = new self();
		$select = $orderDetailModel->select();
		$select->order(array('od_id'));	
		return $orderDetailModel->fetchAll($select);
	}	
    public function fetchLatest($count =6 )
    {
        return $this->fetchAll(null, 
            'od_id DESC', $count);
    }
	
	public function deleteODetailByOrderId($oid)
	{
		$orderDetailModel = new self();
		$where = $orderDetailModel->getAdapter()->quoteInto('o_id = ?', $oid);
		if($where) {
			$orderDetailModel->delete($where);
		}else{
			throw new Zend_Exception("Could not delete orderDetail");
		}
	}
}